﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.InfoConfig.Controller;
using MINERP.DAL;

namespace MINERP.InfoConfig
{
    public partial class FrmFolderManager : FrmMDIChildFormTemplate
    {
        private FolderController _controller = new FolderController();
        private FileDirPath _folder = null;
        static FrmFolderManager instance = null;
        public  FrmFolderManager()
        {
            InitializeComponent();
        }
        public static FrmFolderManager CreateInstance
        {
            get
            {
                if (instance == null)
                    instance = new FrmFolderManager();
                return instance;
            }
        }


        private void FrmFolderManager_Load(object sender, EventArgs e)
        {
            //link datagrid
            _controller.Grid =  gridControlFolder;
            _controller.View = gridViewFolder;
            _controller.GetDate();
        }

        private void btnFolderbrowser_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == folderBrowserDialog1.ShowDialog())
            {
                if (folderBrowserDialog1.SelectedPath.Trim().Substring(0, 2) == @"\\"||folderBrowserDialog1.SelectedPath.ToLower().Contains("ftp"))
                {
                    txtFolderPath.Text = folderBrowserDialog1.SelectedPath;
                }
                else
                {
                    MessageBox.Show("请从网上邻居中选择网络路径!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtFolderPath.Text = "";
                }
            }
        }

        private void gridViewFolder_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _folder = (FileDirPath)gridViewFolder.GetFocusedRow();
            if (_folder != null)
            {
                txtdes.Text = _folder.Des;
                txtFolderPath.Text = _folder.FileDir;
            }

        }
        //private void btnAdd_Click(object sender, EventArgs e)
        //{
        //    if (btnAdd.Text != "保存")
        //    {
        //        PrepareAdd(true);
        //        txtFolderPath.Focus();
        //        return;
        //    }
        //    if (CheckData())
        //    {
        //        FileDirPath obj = new FileDirPath();

        //        obj.FileDir = txtFolderPath.Text;
        //        obj.Des = txtdes.Text;
        //        _controller.Add(obj);

        //        //link datagrid
        //        _controller.GetDate();

        //        PrepareAdd(false);
        //        MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        //    }
        //}

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (btnUpdate.Text != "保存")
            {
                PrepareUpdate(true);
                txtFolderPath.Focus();
                return;
            }
            if (CheckData())
            {
                _folder.FileDir = txtFolderPath.Text;
                _folder.Des = txtdes.Text;
                _controller.Update(_folder);

                //link datagrid
                _controller.GetDate();
                PrepareUpdate(false);

                MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtFolderPath.Enabled = false;
            txtdes.Enabled = false;
            btnFolderbrowser.Enabled = false;


            //btnAdd.Text = "添加";
            btnUpdate.Text = "更新";
            btnUpdate.Enabled = true;
            //btnAdd.Enabled = true;
        }

        /// <summary>
        /// 检查数据完整性
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            bool flag = true;
            if (txtFolderPath.Text == string.Empty)
            {
                txtFolderPath.Focus();
                MessageBox.Show("文件路径不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtdes.Text == string.Empty)
            {
                txtdes.Focus();
                MessageBox.Show("文件夹路径描述不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return flag;
        }
        /// <summary>
        /// 添加前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareAdd(bool bl)
        {
            txtFolderPath.Enabled = bl;
            txtdes.Enabled = bl;
            btnFolderbrowser.Enabled = bl;

            txtFolderPath.Text = string.Empty;
            txtdes.Text = string.Empty;

            if (bl)
            {

                //btnAdd.Text = "保存";
                btnUpdate.Enabled = false;

            }
            else
            {
                //btnAdd.Text = "添加";
                btnUpdate.Enabled = true;
            }
        }

        /// <summary>
        /// 更新前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareUpdate(bool bl)
        {

            txtFolderPath.Enabled = bl;
            txtdes.Enabled = bl;
            btnFolderbrowser.Enabled = bl;

            if (bl)
            {

                btnUpdate.Text = "保存";
                //btnAdd.Enabled = false;

            }
            else
            {
                btnUpdate.Text = "更新";
                //btnAdd.Enabled = true;
            }
        }

        private void FrmFolderManager_FormClosed(object sender, FormClosedEventArgs e)
        {
            instance = null;
        }

    }
}
